<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #fff9e6;
            margin: 0;
            padding: 20px;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
        }
        
        .register-container {
            background: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            width: 100%;
            max-width: 400px;
        }
        
        .title {
            text-align: center;
            color: #5c3c10;
            margin-bottom: 30px;
            font-size: 24px;
        }
        
        .form-group {
            margin-bottom: 20px;
        }
        
        .form-group label {
            display: block;
            color: #5c3c10;
            margin-bottom: 5px;
        }
        
        .form-group input {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        
        .captcha-group {
            display: flex;
            gap: 10px;
        }
        
        .captcha-group input {
            flex: 1;
        }
        
        .captcha-image {
            width: 120px;
            height: 40px;
            border: 1px solid #ddd;
            cursor: pointer;
        }
        
        .register-btn {
            width: 100%;
            padding: 12px;
            background: #ffd700;
            color: #5c3c10;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
            font-weight: bold;
        }
        
        .register-btn:hover {
            background: #ffed4a;
        }
        
        .login-link {
            text-align: center;
            margin-top: 20px;
        }
        
        .login-link a {
            color: #1890ff;
            text-decoration: none;
        }
        
        .error-message {
            color: #ff4d4f;
            font-size: 14px;
            margin-top: 5px;
            display: none;
        }
    </style>
</head>
<body>
    <div class="register-container">
        <h2 class="title">注册</h2>
        
        <div class="form-group">
            <label>用户名</label>
            <input type="text" id="username" placeholder="请输入用户名（不能重复）">
            <div class="error-message" id="username-error"></div>
        </div>
        
        <div class="form-group">
            <label>设置密码</label>
            <input type="password" id="password" placeholder="请输入密码">
            <div class="error-message" id="password-error"></div>
        </div>
        
        <div class="form-group">
            <label>确认密码</label>
            <input type="password" id="confirmPassword" placeholder="请再次输入密码">
            <div class="error-message" id="confirm-password-error"></div>
        </div>
        
        <div class="form-group">
            <label>手机号</label>
            <input type="tel" id="phone" placeholder="请输入手机号">
            <div class="error-message" id="phone-error"></div>
        </div>
        
        <div class="form-group">
            <label>验证码</label>
            <div class="captcha-group">
                <input type="text" id="captcha" placeholder="请输入验证码">
                <img src="/kaptcha" class="captcha-image" id="captchaImage" alt="验证码">
            </div>
            <div class="error-message" id="captcha-error"></div>
        </div>
        
        <button class="register-btn" onclick="register()">注册</button>
        
        <div class="login-link">
            已有账号？<a href="login.html">返回登录</a>
        </div>
    </div>

    <script>
    $(document).ready(function() {
        // 点击验证码图片刷新
        $('#captchaImage').click(function() {
            this.src = '/kaptcha?' + new Date().getTime();
        });
    });

    function register() {
        // 清除所有错误提示
        $('.error-message').hide();
        
        // 获取表单数据
        const username = $('#username').val().trim();
        const password = $('#password').val();
        const confirmPassword = $('#confirmPassword').val();
        const phone = $('#phone').val().trim();
        const captcha = $('#captcha').val().trim();
        
        // 表单验证
        let hasError = false;
        
        if (!username) {
            $('#username-error').text('请输入用户名').show();
            hasError = true;
        }
        
        if (!password) {
            $('#password-error').text('请输入密码').show();
            hasError = true;
        } else if (password.length < 6) {
            $('#password-error').text('密码长度不能少于6位').show();
            hasError = true;
        }
        
        if (password !== confirmPassword) {
            $('#confirm-password-error').text('两次输入的密码不一致').show();
            hasError = true;
        }
        
        if (!phone) {
            $('#phone-error').text('请输入手机号').show();
            hasError = true;
        } else if (!/^1[3-9]\d{9}$/.test(phone)) {
            $('#phone-error').text('请输入正确的手机号').show();
            hasError = true;
        }
        
        if (!captcha) {
            $('#captcha-error').text('请输入验证码').show();
            hasError = true;
        }
        
        if (hasError) {
            return;
        }
        
        // 提交注册请求
        $.ajax({
            url: '/user/register',
            type: 'POST',
            contentType: 'application/json',
            data: JSON.stringify({
                username: username,
                password: password,
                phone: phone,
                captcha: captcha
            }),
            success: function(response) {
                console.log(response);
                if (response.state === 1) {
                    alert('注册成功！');
                    location.href = 'login.html';
                } else {
                    // 显示错误信息到对应的错误提示区域
                    if (response.message.includes('用户名已存在')) {
                        $('#username-error').text(response.message).show();
                    } else if (response.message.includes('验证码')) {
                        $('#captcha-error').text(response.message).show();
                        // 如果是验证码错误，只清空验证码输入框
                        $('#captcha').val('');
                    } else {
                        alert(response.message);
                    }
                    // 刷新验证码
                    $('#captchaImage').click();
                }
            },
            error: function(xhr) {
                let errorMsg = '注册失败，请稍后重试';
                try {
                    const response = JSON.parse(xhr.responseText);
                    if (response.message) {
                        errorMsg = response.message;
                    }
                } catch(e) {
                    console.error('解析错误响应失败:', e);
                }
                alert(errorMsg);
                // 刷新验证码
                $('#captchaImage').click();
                // 清空验证码输入框
                $('#captcha').val('');
            }
        });
    }
    </script>
</body>
</html>
